// pages/archive/archive.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    archiveData: [],
    fullLoading: true,
    total: 0,
    pageIndex: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "归档"
    })
    this.getArchiveData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      pageIndex: 1,
      total: 0,
      archiveData: [],
      fullLoading: true
    });
    that.getArchiveData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onShareTimeline(res) {
    return {
      title: config.miniName + '-归档',
      path: '/pages/archive/archive'
    }
  },

  getArchiveData: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/article/getArchive',
      method: 'GET',
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: 9999
      },
      success(res) {
        if (res.data.code === 200) {
          that.setData({
            archiveData: that.data.archiveData.concat(res.data.data),
            total: res.data.total,
            fullLoading: false
          });
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: res.data.msg
          })
        }
      },
      fail(err) {
        that.setData({
          fullLoading: false
        });
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },
  goArticle: function (event) {
    wx.navigateTo({
      url: "/pages/article/article?id=" + event.currentTarget.dataset.id
    })
  }
})